#
#
import os
import sys


FS = 1
DRIVER = 2
CRYPTO = 3
SYSTEM = 4
NETWORK = 5
OTHER = 6
INPUT = 7
BUS = 8
VIDEO = 9
APP = 10
	
# Type, Oggetti, nome, descrizione, dipendenze (nome), #define
content_list = [
				[INPUT, ["driver/input/keyboard.o"], "keyboard", "standard ps2 keyboard", [], ["DKEYBOARD"]],
				[INPUT, ["driver/input/mouse.o"], "mouse", "standard ps2 mouse", [], ["DMOUSE"]],
				[DRIVER, ["driver/video/video.o"], "video", "standard vga text", [], ["DVIDEO"]],
				[NETWORK, ["driver/net/dp8390.o"], "dp8390", "dp8390 network nic family chip", [], ["DDP8390"]],
				[BUS, ["driver/bus/pci.o"], "pci", "peripheral component interconnect", [], ["DPCI"]],
				
				[NETWORK, ["driver/net/rs232.o"], "rs232", "serial network standard", [], ["DRS232"]],
				
				[VIDEO, ["driver/video/vesa.o"], "vesa", "vesa video driver", [], ["DVESA", "VESA", "VBE"]],

				[OTHER, ["driver/db/sqlite.o driver/db/sqlite_start.o"], "sqlite", "sqlite server", [], ["SQL", "SQLITE"], "-DSQLITE_OS_OTHER=1 -DSQLITE_THREADSAFE=0"],
				
				[NETWORK, ["network/network.o"], "network", "network stack", [], ["NETWORK"]],
				[NETWORK, [], "ipv4", "ipv4 network protocol", ["network"], ["IPV4"]],
				[NETWORK, ["network/dns.o"], "dns", "domain name resolving", ["ipv4"], ["DNS"]],
					
				[CRYPTO, ["driver/crypto/crc32.o"], "crc32", "crc checksum algorithm", [], ["DCRC32"]],
				[CRYPTO, ["driver/crypto/md5.o"], "md5", "md5 checksum algorithm", [], ["DMD5"]],
					
				[SYSTEM, ["bish.o"], "bish", "built in shell", ["keyboard", "pci"], ["SHELL"]],
				[SYSTEM, ["debug.o"], "debug", "kernel debug", [], ["DEBUG"]],
					
				[FS, ["driver/fs/pfs/pfs.o"], "pigfs", "spiderpig file system", [], ["FSPFS"]],
				[FS, ["driver/fs/tmpfs/tmpfs.o"], "tmpfs", "ram file system", [], ["FSTMPFS"]],
				[FS, ["driver/fs/initramfs/initramfs.o"], "initramfs", "initial ram file system", [], ["FSINITRAMFS"]],

				[APP, ["screenpig/main.o", "screenpig/render.o", "screenpig/events.o", "screenpig/loading.o", "screenpig/term.o", "screenpig/bar.o", "screenpig/window.o"], "screen pig", "kernel level simple gui", ["vesa", "keyboard", "mouse"], ["SCREENPIG", "VESA"]]
			  ]
					
arch_list = [["x86", "", ["X86", "BIT32"], 
				["arch/x86/io.o", "arch/x86/dt.o", "arch/x86/arch.o", "arch/x86/timer.o",
				"arch/x86/clock.o", "arch/x86/mm.o", "arch/x86/task.o", "arch/x86/asm_calls.o",
				"arch/x86/cpu.o", "arch/x86/asm_math.o"]], ["x86_64", "not yet full implemented", ["X86_64", "BIT64"], 
				["arch/x86_64/arch.o"]], ["sparc", "not yet full implemented", ["SPARC", "BIT32"], 
				["arch/sparc/arch.o"]]]


languages = [["italiano", "lang/italian.h"],
	 		 ["english", "lang/english.h"],
	 		 ["sardo", "lang/sardinian.h"]]
#os.system("clear")


def add_content(name, ask, selecteds):
	x = []
	if name in selecteds:
		return selecteds
		
	for a in content_list:
		if a[2] == name:
			x = a
	if x == []: return

	if ask:
		print "          -> insert " + x[2] + " ( " + x[3] + " - ",
		for i in x[1]:
			print i,
		print ")?",
		r = raw_input()
		if r == 'y' or r == 'Y':
			if x[4] != []:
				for a in x[4]:
					selecteds = add_content(a, 0, selecteds);
			selecteds.append(x[2])
	
	else:
		print "          -> inserted " + x[2] + " ( " + x[3] + " - ",
		for i in x[1]:
			print i,
		print " )"
		if x[4] != []:
			for a in x[4]:
				selecteds = add_content(name, 0, selecteds);
		
		selecteds.append(x[2])
				
	return selecteds


print " -> kernel config"
c = raw_input(" -> select a preset (y/n)? ")
if c == 'y':
	preset_list = [	["x86-generic32", "generic x86 32bit set"],
	 						["x86-generic64", "generic x86 64bit set"]]
	
else:
	print "     -> kernel options selection"
	print "     -> done"

	print "     -> kernel builtin selection"
	selected = []
	

	print "       -> input"
	for x in content_list:
		if x[0] == INPUT:
			selected = add_content(x[2], 1, selected)
			
	print "       -> video"
	for x in content_list:
		if x[0] == VIDEO:
			selected = add_content(x[2], 1, selected)			
			
	print "       -> bus"
	for x in content_list:
		if x[0] == BUS:
			selected = add_content(x[2], 1, selected)


	print "       -> filesystem"
	for x in content_list:
		if x[0] == FS:
			selected = add_content(x[2], 1, selected)			

	print "       -> network"
	for x in content_list:
		if x[0] == NETWORK:
			selected = add_content(x[2], 1, selected)	
			
	print "       -> crypto"
	for x in content_list:
		if x[0] == CRYPTO:
			selected = add_content(x[2], 1, selected)
			
	print "       -> system"
	for x in content_list:
		if x[0] == SYSTEM:
			selected = add_content(x[2], 1, selected)
			
			
	print "       -> app"
	for x in content_list:
		if x[0] == APP:
			selected = add_content(x[2], 1, selected)
			
	print "       -> other"
	for x in content_list:
		if x[0] == OTHER:
			selected = add_content(x[2], 1, selected)	
			
			
	# Aggiunge gli oggetti e i define
	flags = "EXFLAGS = "
	builtin = "BUILTIN = "
	config = "#ifndef CONFIG_H\n#define CONFIG_H\n\n"


	print "     -> Language selction: "
	a = 0
	for x in languages:
		print "     	-> "+str(a)+": "+str(x[0])+" ("+x[1]+")"
		a += 1
	a = str(raw_input("     -> 0/"+str(a-1)+": "))
	config += "#include <" + languages[int(a)][1] + ">\n"
	
					
	print "     -> Arch selection: "
	a = 0
	for x in arch_list:
		print "     	-> "+str(a)+") "+x[0]+" ("+x[1]+")"
		a += 1
	a = str(raw_input("     -> 0/"+str(a-1)+": "))
	
	for x in arch_list[int(a)][3]:
		builtin += x + "\\\n"
		
	for x in arch_list[int(a)][2]:
		config += "#define " + x + "\n"
	
	for x in selected:
		for a in content_list:
			if a[2] == x:
				for b in a[1]:
					builtin += b + "\\\n"
				for b in a[5]:
					config += "#define " + b + "\n"
				
				try:
					flags += " "+a[6]+" "
				except:
					pass
					
	config += "\n\n#endif\n"
	print "     -> kernel options selection"
	
	
	
	
	# Scrive tutti i dati nei file di configurazione
	a = open("configs/current.am", "w")
	a.write(builtin+"\n\n"+flags+"\n\n")
	a.close()
	print "     -> configs/current.am wrote"
		
	a = open("configs/current_config.h", "w")
	a.write(config)
	a.close()
	print "     -> configs/current_config.h wrote"
	
	os.system("cp configs/current_config.h include/config.h")
	print "     -> include/config.h wrote"
	
	os.system("make clean > /dev/null")
	print "     -> .o cleaned"


	# Compile
	print "     -> compile all? ",
	r = raw_input()
	if r == 'y' or r == 'Y':
		os.system("make")
	
